<?php

namespace app\api\home;

use app\api\model\ImBlack;
use app\api\model\ImFriendGroup;
use app\api\model\ImFriendGroupUser;
use app\api\validate\BlackValidate;
use app\api\validate\FriendGroupValidate;
use app\api\validate\FriendGroupUserValidate;
use app\service\im\User as ImUser;
use app\api\validate\FriendValidate;
use app\api\model\ImFriend;

class FriendController extends Base
{
    //注册腾讯im用户
    public function accountImport()
    {
        $params = $this->request->post();
        // 验证
        $validate = new FriendValidate;
        if (!$validate->scene('add')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $res = new ImUser();
        $data = $res->accountImport(strval($this->uid), $params['nick'], $params['FaceUrl']);

        if ($data['ActionStatus'] == 'OK') {
            return_ajax(1, '操作成功', $data);
        } else {
            return_ajax(0, '操作失败', $data);
        }
    }

    //添加好友
    public function addFriend()
    {
        $params = $this->request->post();
        // 验证
        $validate = new FriendValidate;
        if (!$validate->scene('addFriend')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $res = new ImUser();
        $data = $res->friendAdd(strval($this->uid), $params['touid'], 'Tag_SNS_IM_Remark');

        if ($data['ActionStatus'] == 'OK') {
            $result = (new ImFriend)->addFriend(strval($this->uid), $params['touid'], $params['group_id']);//添加好友
//            $re = (new ImFriendGroupUser)->addFriendGroupUserOne($params['touid'],$params['group_id']);//记录好友分组用户

            if ($result !== false) {
                return_ajax(1, '操作成功', $result);
            }
            return_ajax(0, '好友入库失败', $result);

        } else {
            return_ajax(0, '添加腾讯好友失败');
        }
    }

    //好友列表
    public function friendList()
    {
        $params = $this->request->get();

        // 验证
        $validate = new FriendValidate;
        if (!$validate->scene('list')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $list = (new ImFriend)->friendList($this->uid, $params);
        $res = $list->items();  //列表

        $data['list'] = $res;
        $data['page'] = $params['page'];
        $data['count'] = $list->total();//总条数

        return_ajax(1, '获取成功', $data);
    }

    //删除好友
    public function delFriend()
    {
        $params = $this->request->post();
        // 验证
        $validate = new FriendValidate;
        if (!$validate->scene('delFriend')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $res = new ImUser();
        $data = $res->delFriend(strval($this->uid), $params['touid']);
        if ($data['ActionStatus'] == 'OK') {
            $result = (new ImFriend)->delFriend($this->uid, $params['touid']);
            if ($result) {
                return_ajax(1, '操作成功', $result);
            }
            return_ajax(0, '数据库更新失败', $result);

        } else {
            return_ajax(0, '删除腾讯好友失败', $data);
        }
    }

    //添加黑名单
    public function addBlack()
    {
        $params = $this->request->post();
        // 验证
        $validate = new BlackValidate;
        if (!$validate->scene('addBlack')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $res = new ImUser();
        $data = $res->blackListAdd(strval($this->uid), $params['black_uid']);
        if ($data['ActionStatus'] == 'OK') {
            $result = (new ImBlack)->addBlack($this->uid, $params['black_uid']);
            if ($result) {
                return_ajax(1, '操作成功', $result);
            }
            return_ajax(0, '数据库更新失败', $result);

        } else {
            return_ajax(0, '腾讯加入黑名单失败', $data);
        }
    }

    //黑名单列表
    public function blackList()
    {
        $params = $this->request->get();

        // 验证
        $validate = new BlackValidate;
        if (!$validate->scene('list')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $list = (new ImBlack)->GetBlackList($params);
        $res = $list->items();  //列表

        $data['list'] = $res;
        $data['page'] = $params['page'];
        $data['count'] = $list->total();//总条数

        return_ajax(1, '获取成功', $data);
    }

    //删除黑名单
    public function delBlack()
    {
        $params = $this->request->post();
        // 验证
        $validate = new BlackValidate;
        if (!$validate->scene('delBlack')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $res = new ImUser();
        $data = $res->delFriend(strval($this->uid), $params['black_uid']);
        if ($data['ActionStatus'] == 'OK') {

            $result = (new ImBlack)->DelBlack($this->uid, $params['black_uid']);

            if ($result) {
                return_ajax(1, '操作成功', $result);
            }
            return_ajax(0, '数据库更新失败', $result);
        } else {
            return_ajax(0, '腾讯删除黑名单失败', $data);
        }
    }


    /**
     * Notes:批量添加好友分组
     * User: tangfen
     * Date: 2023/12/23
     * Time: 14:09
     */
    public function addGroupFriends()
    {
        $params = $this->request->post();
        // 验证
        $validate = new  FriendGroupValidate;
        if (!$validate->scene('add')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
//        $res = new ServiceGroup();
//        $data = $res->addGroupFriend($params['uid'], $params['friend_group_name'],$params['touid']);


//        if ($data['ActionStatus'] == 'OK'){

        $friendGroupId = (new ImFriendGroup)->addFriendGroup($params);//记录好友分组

        $params['friend_group_id'] = $friendGroupId;
        $result = (new ImFriendGroupUser)->addFriendGroupUser($params);//记录好友分组用户
        if ($result) {
            return_ajax(1, '操作成功', $result);
        }
        return_ajax(0, '操作失败', $result);
//        } else {
//            result_error();
//        }
    }


    /**
     * Notes:好友分组列表
     * User: tangfen
     * Date: 2023/12/23
     * Time: 15:44
     */
    public function groupFriendList()
    {
        $params = $this->request->get();

        // 验证
        $validate = new FriendGroupValidate;
        if (!$validate->scene('list')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $list = (new ImFriendGroup)->groupFriendList($params);
        $res = $list->items();  //列表

        $data['list'] = $res;
        $data['page'] = $params['page'];
        $data['count'] = $list->total();//总条数
        return_ajax(1, '操作成功', $data);
    }


    /**
     * Notes:删除好友分组
     * User: tangfen
     * Date: 2023/12/23
     * Time: 15:57
     */
    public function delGroupFriend()
    {
        $params = $this->request->post('');
        // 验证
        $validate = new FriendGroupValidate;
        if (!$validate->scene('del')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $result = (new ImFriendGroup)->delGroupFriend($params['id']);

        if ($result) {
            return_ajax(1, '操作成功', $result);
        }
        return_ajax(0, '操作失败', $result);
    }

    /**
     * Notes:添加好友分组
     * User: tangfen
     * Date: 2023/12/23
     * Time: 16:05
     */
    public function addGroupFriend()
    {
        $params = $this->request->post();
        // 验证
        $validate = new  FriendGroupValidate;
        if (!$validate->scene('addOne')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;

        $result = (new ImFriendGroup)->addFriendGroup($params);//记录好友分组

        if ($result) {
            return_ajax(1, '操作成功', $result);
        }
        return_ajax(0, '操作失败', $result);
    }


    /**
     * Notes:修改用户分组
     * User: tangfen
     * Date: 2023/12/23
     * Time: 16:05
     */
    public function updateUserGroup()
    {
        $params = $this->request->post();
        // 验证
        $validate = new  FriendGroupUserValidate;
        if (!$validate->scene('add')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $result = (new ImFriend)->updateUserGroup($params);//修改用户分组
        if ($result) {
            return_ajax(1, '操作成功', $result);
        }
        return_ajax(0, '操作失败', $result);
    }

    /**
     * Notes:移除用户分组
     * User: tangfen
     * Date: 2023/12/23
     * Time: 16:18
     */
    public function delUserGroup()
    {
        $params = $this->request->post();
        // 验证
        $validate = new FriendGroupUserValidate;
        if (!$validate->scene('del')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $result = (new ImFriend)->delUserGroup($params);//删除用户分组

        if ($result) {
            return_ajax(1, '操作成功', $result);
        }
        return_ajax(0, '操作失败', $result);
    }

    /**
     * Notes:获取分组用户列表
     * User: tangfen
     * Date: 2023/12/23
     * Time: 16:23
     */
    public function groupFriendUserList()
    {

        $params = $this->request->get();

        // 验证
        $validate = new FriendGroupUserValidate;
        if (!$validate->scene('list')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $list = (new ImFriendGroupUser)->groupFriendUserList($params);
        $res = $list->items();  //列表

        $data['list'] = $res;
        $data['page'] = $params['page'];
        $data['count'] = $list->total();//总条数
        return_ajax(1, '操作成功', $data);
    }


}